/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.oea.servlets;

import com.oea.hibernate.entities.Exam;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author unnati
 */
public class EditExamAction extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
           
            HttpSession s=request.getSession();
            
            Long id=Long.parseLong(request.getParameter("id"));
            List<Exam> list_exam=(List<Exam>) request.getSession().getAttribute("li_allexams");
            
            for (Exam e : list_exam)
            {   
                if(id==e.getId())
                {
                    System.out.println(e.getBranch().getName());
                    String sdate=e.getSdate();
                    String edate=e.getEdate();
                    
                    String[] startdate=sdate.split("-");
                    String[] enddate=edate.split("-");
                    
                    s.setAttribute("edate_day", enddate[0]);
                    s.setAttribute("edate_month", enddate[1]);
                    s.setAttribute("edate_year", enddate[2]);

                    s.setAttribute("sdate_day", startdate[0]);
                    s.setAttribute("sdate_month", startdate[1]);
                    s.setAttribute("sdate_year", startdate[2]);
                    
                    s.setAttribute("detail", e);
                    break;
                    
                }
                
            }
            RequestDispatcher rd=request.getRequestDispatcher("edit-exam.jsp");
            rd.forward(request, response);
//            response.sendRedirect("edit-exam.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
